﻿
(function () {
    //定义服务
    var _testService = abp.services.app.test;

    //定义变量
    var $mgrid = $('#dg');//datagrid
    var $mgtbr = $('#tb');//toolbar
    var $mgdlg = $('#dd');//dialog

    

    function init(objs){
        InitMainGridToolbar(objs);
        InitDataGrid(objs);
        CreateATest();//创建按键绑定
        DeleteATest();
    }
    
    function InitMainGridToolbar(objs){
        $mgtbr.find('.btn-create').linkbutton({ iconCls: "icon-add", plain: true });
        $mgtbr.find('.btn-delete').linkbutton({iconCls:"icon-remove", plain:true})
    }

    function InitDataGrid(objs){
        $mgrid.datagrid({
            abpMethod: _testService.getPagedTests,
            emptyMsg: '没有找到记录...',
            loadMsg: '正在加载数据...',
            width: 720,
            height: 320,
            pageSize: 15,
            pageList: [1,15, 20, 30, 40, 50],
            pagination: true,
            striped: true,
            singleSelect: true,
            rownumbers: true,
            remoteSort: false,
            toolbar: $mgtbr,
            columns: [[
                { field: 'id', title: '主键', width: 100 },
                { field: 'name', title: '名字', width: 100 },
                { field: 'age', title: '年龄', width: 100, align: 'right' }
            ]],
            onDblClickRow: function (index, row) {
                var options = {
                    element: $mgdlg,
                    title: '编辑用户',
                    url: '/Test/CreateOrEditModal',
                    id: row.id,
                }
                fopendialog(options);
            }
        });

    }
    function RefreshDataGrid(){
        $mgrid.datagrid('reload');
    }

    function CreateATest(objs){
        $mgtbr.find('.btn-create').click(function(){
            var options = {
                element: $mgdlg,
                title: '新建用户',
                url: '/Test/CreateOrEditModal',
                id: undefined,
            }
            fopendialog(options);
        });
    }

    function DeleteATest(){
        $mgtbr.find('.btn-delete').click(function(){
            var selectedRow = $mgrid.datagrid('getSelected');
            if(selectedRow ==undefined){
                return;
            }
            console.log(selectedRow);
            abp.message.confirm("确定删除这个条目吗？", function(isConfirm){
                if(isConfirm){
                    _testService.deleteTestById({id:selectedRow.id}).done(function(){
                        RefreshDataGrid();
                        abp.notify.success("删除成功");
                    });
                }
            });
        });
    }

    function finitdialog(objs){
        //初始化下拉框等
    }

    /*
    options中应该有的属性：
        objs:??
        options.url:请求对话框的网页地址
        options.id:请求的id信息
        options.element = $('#dialog');
        options.title:标题
        additional: bool????
    */
    function saveTest(options){
        var _$form = options.element.find('.fm');
        if (!_$form.form('validate')) {
            return;
        }
        abp.ui.setBusy(options.element.parent());
        var mine = _$form.serializeFormToObject();
        _testService.createOrUpdateTest({TestEditDto: mine}).done(function () {
            options.element.dialog('close');
            RefreshDataGrid();
            abp.notify.success("保存成功！");
        }).always(function () {
            abp.ui.clearBusy(options.element.parent());
        });
    }
    function fopendialog(options){
        if(options.id != undefined){
            options.url += "?id="+options.id;
        }
        abp.ajax({
            url: options.url,
            //data: JSON.stringify({ Id: options.id }),
            dataType: null,
            type:'get'
        }).done(function(data){
            options.element.dialog({
                content: data,
                //queryParams: { id: options.id },
                loadingMessage: "正在加载...",
                title: options.title,
                width: 780,
                height: 520,
                closed: false,
                modal: true,
                buttons: [{
                        text: '保存',
                        iconCls: 'icon-save',
                        handler: function () {
                            saveTest(options);
                        }
                    }, {
                        text: '取消',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            options.element.dialog('close');
                        }
                    }],
                onLoad: function () {
                    // finitdialog(options);
                },
                onClose: function () {
                    $(this).dialog('destroy');
                }
            });
            finitdialog(options);
        });


    }

    init(this);

    /*
    $('#btn').linkbutton({
        iconCls: 'icon-add'
    }).bind('click', function () {
        //var id = 1;//模拟读出的id
        url = '/Test/CreateOrEditModal';
        
        $('#dd').dialog({
            title: 'My Dialog',
            width: 400,
            height: 200,
            closed: false,
            collapsible: true,
            cache: false,            
            href: url,
            modal: true
        });

    });
    */
    
})();